package br.edu.ufcg.dsc.psoo.mymoney.view.action;

import org.mentawai.action.BaseLoginAction;

import br.edu.ufcg.dsc.psoo.mymoney.controller.AccountController;
import br.edu.ufcg.dsc.psoo.mymoney.controller.MovementController;
import br.edu.ufcg.dsc.psoo.mymoney.controller.UserController;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.InvalidUserException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.implementation.AccountControllerImpl;
import br.edu.ufcg.dsc.psoo.mymoney.controller.implementation.MovementControllerImpl;
import br.edu.ufcg.dsc.psoo.mymoney.controller.implementation.UserControllerImpl;
import br.edu.ufcg.dsc.psoo.mymoney.model.bean.User;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.AccountDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.GroupDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.MovementDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.TypeDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.UserDAO;

public class LoginAction extends BaseLoginAction {

	private UserController controller;
	private AccountController accountController;
	private MovementController movementController;
	private AccountDAO accountDAO;
	private UserDAO userDAO;
	private MovementDAO movementDAO;
	private TypeDAO typeDAO;
	private GroupDAO groupDAO;

	/**
	 * Injection do Mentawai
	 * 
	 * @param userDAO
	 */
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	// TODO javadoc
	public void setAccountDAO(AccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}
	// TODO javadoc
	public void setMovementDAO(MovementDAO movementDAO) {
		this.movementDAO = movementDAO;
	}
	// TODO javadoc
	public void setTypeDAO(TypeDAO typeDAO) {
		this.typeDAO = typeDAO;
	}
	// TODO javadoc
	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public void initiate() {
		if (accountController == null)
			accountController = new AccountControllerImpl(accountDAO);
		if (movementController == null)
			movementController = new MovementControllerImpl(movementDAO,
					userDAO, groupDAO, accountDAO, typeDAO);
		if (controller == null)
			controller = new UserControllerImpl(userDAO,accountDAO);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mentawai.core.BaseAction#execute()
	 */
	/**
	 * Sobrescrita do método padrão de LoginAction do Mentawai
	 * 
	 * @return String - veredicto
	 */
	@Override
	public String execute() {
		initiate();
		if (!isPost()) {
			addError("default", "usuario nao logado!");
			return ERROR;
		}

		String username = input.getString("username");

		String password = input.getString("password");

		User loggedUser = null;
		try {
			loggedUser = controller.log(username, password);
			loggedUser.setConfirmation(loggedUser.getPassword());
			setUserSession(loggedUser);

			return SUCCESS;
		} catch (InvalidUserException e) {
			// algum erro na validação dos dados do input
			addError("defaultError", e.getMessage());
			return ERROR;
		}
	}
}